package com.example.demo.controller;

import com.alibaba.druid.util.StringUtils;
import com.example.demo.bean.GoodsDetailSeckillStatus;
import com.example.demo.bean.GoodsSeckill;
import com.example.demo.bean.SeckillStatus;
import com.example.demo.bean.User;
import com.example.demo.dao.GoodsDao;
import com.example.demo.dao.OrderDao;
import com.example.demo.exception.CodeMsg;
import com.example.demo.exception.GlobalException;
import com.example.demo.redis.RedisServer;
import com.example.demo.redisPrefix.GoodsPrefixAndExpireSecond;
import com.example.demo.result.Result;
import com.example.demo.service.GoodsService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.spring4.context.SpringWebContext;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;


    @Autowired
    RedisServer redisServer;

    @Autowired
    GoodsDao goodsDao;

    @RequestMapping(value = {"/to_list"},produces = "text/html")
    @ResponseBody//将json的html页面进行展示
    public String goodsList(User user, Model model, HttpServletRequest request, HttpServletResponse response){

        System.out.println(user);
        //一定不要在controller层内写过多代码
        String goodsListHtml = goodsService.goodsService(user, model, request, response);
        return goodsListHtml;
    }

    @RequestMapping("/detail/{goodsId}")
    @ResponseBody
    public Result<GoodsDetailSeckillStatus> showGoodsDetail(@PathVariable("goodsId")Long goodsId,User user,Model model){
        System.out.println(goodsId);
        //去数据库寻找对应的信息
        GoodsSeckill goodsSeckillAllInfro = goodsDao.findGoodsSeckillAllInfro(goodsId);

        //下面进行日期比较(与当前的毫秒值进行比较)
        long startTime = goodsSeckillAllInfro.getStartDate().getTime();
        long endTime = goodsSeckillAllInfro.getEndDate().getTime();
        Integer seckillStatus;//秒杀状态
        Long lastTime;//剩余时间(秒）
        //当下时间
        long nowTime = new Date().getTime();
        if (startTime>nowTime){
            //秒杀未开始
            seckillStatus=SeckillStatus.seckillNotStart.getCode();
            lastTime=(startTime-nowTime)/1000;
        }else if (endTime<nowTime){
            //秒杀已经结束
            seckillStatus=SeckillStatus.seckillOver.getCode();
            lastTime=-1l;
        }else {
            //秒杀进行中startTime<nowTime<endTime
            seckillStatus=SeckillStatus.seckilling.getCode();
            lastTime=0l;
        }

        //把user存入request域中，因为待会会用到
        model.addAttribute("user",user);
        GoodsDetailSeckillStatus gSeckillStatus=new GoodsDetailSeckillStatus();
        gSeckillStatus.setGoodsSeckill(goodsSeckillAllInfro);
        gSeckillStatus.setUser(user);
        gSeckillStatus.setRemainSeconds(lastTime);
        gSeckillStatus.setSeckillStatus(seckillStatus);


        return Result.success(gSeckillStatus);
    }

    //进行正式数据库系统秒杀
}
